package lesson11;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;
import java.util.concurrent.TimeUnit;

/**
 * @author tongchen
 * @create 2023-02-07 19:56
 */
public class TestCallable {
    public static void main(String[] args) {
        Callable<Integer> callable = new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                try {

                }catch (RuntimeException runtimeException){

                }
                int a = 10;
                int b = 20;
                System.out.println("等待计算结果.....");
                TimeUnit.SECONDS.sleep(3);
                throw new RuntimeException("测试异常.....");
               // return a + b;

            }
        };

        //futureTask负责接收call方法的返回值
        FutureTask<Integer> integerFutureTask = new FutureTask<>(callable);
        Thread t1=new Thread(integerFutureTask);
        t1.start();
        try {
            Integer result = integerFutureTask.get();
            System.out.println("result="+result);
        } catch (InterruptedException e) {
            e.printStackTrace();
          //  throw new RuntimeException(e);
        } catch (ExecutionException e) {
           // throw new RuntimeException(e);
        }

    }
}
